package com.tbit.main.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.tbit.main.constant.BrandRemoteConfigConstant;
import com.tbit.main.constant.MachineAbnormalConstant;
import com.tbit.main.constant.enums.MachineAlarmEnum;
import com.tbit.main.controller.interceptor.LoginUserContextHolder;
import com.tbit.main.dao.core.AccountUserAlarmItemDao;
import com.tbit.main.dao.core.MachineAlarmDao;
import com.tbit.main.exception.BaseException;
import com.tbit.main.pojo.AccountSmsModel;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.BrandRemoteConfig;
import com.tbit.main.pojo.MachineAlarm;
import com.tbit.main.pojo.bo.AccountUserAlarmItemBO;
import com.tbit.main.service.*;
import com.tbit.main.util.LogUtil;
import com.tbit.main.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author LMY
 * @create 2020-11-16 11:42
 * 资产预警开关
 */
@Service("machineAlarmService")
public class MachineAlarmServiceImpl implements MachineAlarmService {
    @Autowired
    private MachineAlarmDao machineAlarmDao;
    @Autowired
    private MachineAbnormalService machineAbnormalService;
    @Autowired
    private AccountService accountService;
    @Autowired
    private AccountSmsModelService accountSmsModelService;
    @Autowired
    private BrandRemoteConfigService brandRemoteConfigService;
    @Autowired
    private AccountUserAlarmItemDao accountUserAlarmItemDao;

    @Override
    public void insert(MachineAlarm machineAlarm) {
        this.checkHasSmsConfig(machineAlarm.getAccountId());

        AccountUser accountUser = LoginUserContextHolder.getLoginUser();
        machineAlarm.loadDefaultField();
        LogUtil.info("修改资产预警：" + accountUser.getPhone() + ",新增为：" + machineAlarm);

        machineAlarmDao.insert(machineAlarm);
    }

    @Override
    public void update(MachineAlarm machineAlarm, MachineAlarm machineAlarmDb) {
        this.checkHasSmsConfig(machineAlarm.getAccountId());

        AccountUser accountUser = LoginUserContextHolder.getLoginUser();
        ObjectUtil.assign(machineAlarm, machineAlarmDb, null);
        LogUtil.info("修改资产预警：" + accountUser.getPhone() + ",原数据：" + machineAlarmDb + ",修改为：" + machineAlarm);

        if (machineAlarm.getBatterLockSwitch() != null && machineAlarm.getBatterLockSwitch().equals(0)) {
            machineAbnormalService.delAccountId(machineAlarm.getAccountId(), MachineAbnormalConstant.MACHINE_ABNORMAL_DCS);
        }

        deleteClosedAlarmItem(machineAlarm);

        machineAlarmDao.update(machineAlarm);
    }

    /**
     * 删除已关闭的预警项
     */
    @SuppressWarnings("unchecked")
    private void deleteClosedAlarmItem(MachineAlarm machineAlarm) {
        Map<String, Object> map = BeanMap.create(machineAlarm);

        List<Integer> deleteAlarmTypes = new ArrayList<>();
        map.forEach((key, value) -> {
            if (value instanceof Integer) {
                if (Integer.parseInt(value.toString()) == 0) {
                    deleteAlarmTypes.add(MachineAlarmEnum.getTypeByCode(key));
                }
            }
        });

        if (CollUtil.isEmpty(deleteAlarmTypes)) {
            return;
        }

        AccountUserAlarmItemBO alarmTypeBO = AccountUserAlarmItemBO.createAlarmTypeBO(machineAlarm.getAccountId(), deleteAlarmTypes);
        accountUserAlarmItemDao.deleteByAlarmTypes(alarmTypeBO);
    }

    @Override
    public MachineAlarm getByAccountId(Integer accountId) {
        return machineAlarmDao.getByAccountId(accountId);
    }

    @Override
    public List<MachineAlarm> getAll() {
        return machineAlarmDao.getAll();
    }

    @Override
    public List<MachineAlarm> getType(String type) {
        return machineAlarmDao.getType(type);
    }

    @Override
    public void checkHasSmsConfig(Integer accountId) {
        Integer brandId = accountService.getByAccount(accountId);

        if (brandId == null) {
            return;
        }

        AccountSmsModel accountSmsModel = accountSmsModelService.getByAccountIdAndType(brandId, 5);
        Integer templateIdTbit = brandRemoteConfigService.getTemplateIdTbit(brandId, BrandRemoteConfigConstant.TBIT_MACHINE_ALARM);
        BrandRemoteConfig clSms = brandRemoteConfigService.getByAccountIdAndType(brandId, BrandRemoteConfigConstant.CL_SMS);

        // 没有配置短信模板
        if (accountSmsModel == null && templateIdTbit == null && clSms == null) {
            throw new BaseException("System.configure.warning.message");
        }
    }

}
